﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using eLearning.Model;
using eLearning.Util;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;
using System.Text;

namespace eLearning.View
{
    public partial class ThreadPage : PhoneApplicationPage
    {
        private Thread currentForum;

        public ThreadPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            LoadForumThread(false, "");
        }

        private void LoadForumThread(bool search, string json)
        {
            string url = null;

            if (search)
                url = Config.Instance.THREAD_SERVICE_URL() + Config.FIND_ACTION_KEY;
            else
                url = Config.Instance.THREAD_SERVICE_URL() + Config.VIEW_ACTION_KEY;

            string urlformat = string.Format("{0}&{1}", url, Guid.NewGuid().ToString());
            Uri uri = new Uri(urlformat, UriKind.Absolute);

            WebClient webClient = new WebClient();
            webClient.Encoding = Encoding.UTF8;
            webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
            webClient.Headers[HttpRequestHeader.ContentLength] = json.Length.ToString();

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(listThread_UploadStringCompleted);
            webClient.UploadStringAsync(uri, "POST", json);
        }

        private void listThread_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            try
            {
                ThreadCollection list = JsonConvert.DeserializeObject<ThreadCollection>(e.Result);

                if (list == null || list.Count <= 0)
                {
                    listThread.ItemsSource = null;
                    MessageBox.Show("No Thread found");
                }
                else
                {
                    listThread.ItemsSource = list;
                    pivotThread.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void listThread_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                Thread forum = (Thread)e.AddedItems[0];

                currentForum = forum;
                ((ListBox)sender).SelectedIndex = -1;

                Utility.GotoPage(this, "/View/ThreadPostingPage.xaml");
                PhoneApplicationService.Current.State[Config.THREAD] = currentForum;
            }
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            PhoneApplicationService.Current.State[Config.THREAD] = new Thread();
            Utility.GotoPage(this, "/View/ThreadFormPage.xaml");
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            LoadForumThread(false, "");
        }

        private void buttonSearch_Click(object sender, RoutedEventArgs e)
        {
            if (textBoxKeyword.Text.Length < 1)
            {
                MessageBox.Show("Keyword is empty.");
                return;
            }
            Thread find = new Thread();
            find.Judul = textBoxKeyword.Text;
            find.Isi = textBoxKeyword.Text;
            LoadForumThread(true, JsonConvert.SerializeObject(find));
        }

    }
}